package com.kly.chatter.api.application.controller.user;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.kly.chatter.api.application.controller.user.req.MessageReq;
import com.kly.chatter.api.application.controller.user.res.ModelRes;
import com.kly.chatter.api.application.controller.user.res.UserRes;
import com.kly.chatter.api.application.controller.user.res.UsersRes;
import com.kly.chatter.api.application.service.CmFirstMessageRecordService;
import com.kly.chatter.api.application.service.UsersService;
import com.kly.chatter.api.application.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/users")
@Api(value = "/users", tags = "users用户操作")
public class UsersController {

    @Autowired
    private UsersService usersService;

    @Autowired
    private CmFirstMessageRecordService cmFirstMessageRecordService;

    @GetMapping("/getUsers")
    @SentinelResource
    @ApiOperation(value = "读取用户列表", notes = "读取用户列表")
    public Result<UsersRes> getUsers(@RequestParam @ApiParam("chatter userCode") String chatterUserCode) {
        UsersRes usersRes = new UsersRes();
        List<UserRes> userResList = usersService.getUsers(chatterUserCode);
        if (userResList.isEmpty()) {
            usersRes.setMessage("At every full hour, we will offer a certain number of online users for everyone to connect with. Please cherish these opportunities to engage and have meaningful conversations with users.");
        } else {
            usersRes.setUserResList(userResList);
        }
        return Result.ok(usersRes);
    }

    @GetMapping("/getModels")
    @SentinelResource
    @ApiOperation(value = "读取chatter下model", notes = "读取chatter下model")
    public Result<List<ModelRes>> getModels(@RequestParam @ApiParam("chatter userCode") String chatterUserCode,@RequestParam @ApiParam("userCode") String userCode) {
        List<ModelRes> modelResList = usersService.getModels(chatterUserCode, userCode);
        return Result.ok(modelResList);
    }

    @PostMapping("/sendFirstMessage")
    @SentinelResource
    @ApiOperation(value = "cm对用户发送第一条消息", notes = "cm对用户发送第一条消息")
    public Result sendFirstMessage(@RequestBody MessageReq messageReq) {
        cmFirstMessageRecordService.addRecord(messageReq);
        return Result.ok();
    }

    @PostMapping("/canConnect")
    @SentinelResource
    @ApiOperation(value = "是否可以聊天", notes = "是否可以聊天")
    public Result canConnect(@RequestBody MessageReq messageReq) {
        return usersService.canConnect(messageReq);
    }

}
